package com.sf.dlb.web.service;

import com.sf.dlb.bean.AppVersionBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.jpa.model.AppVersionInfo;
import com.sf.dlb.jpa.repository.AppVersionRepository;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.utils.StateToValueUtils;
import com.sf.dlb.vo.AppVersionVO;
import com.sf.dlb.vo.ResultVO;
import com.sf.dlb.web.config.Global;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jing.tan on 2018/5/21.
 */
@Service
public class AppVersionService {
    private static Logger logger = LoggerFactory.getLogger(AppVersionService.class);

    @Autowired
    private AppVersionRepository appVersionRepository;
    @Resource
    private Global global;
    /**
     * 获取APP版本信息列表
     * @return
     */
    public ResultVO findByList(String stystemType){
        logger.info("==findByList stystemType={}",stystemType);
        AppVersionInfo appVersionInfo=new AppVersionInfo();
        appVersionInfo.setDelFlag("0");
        if(StringUtils.isNotBlank(stystemType)){
            appVersionInfo.setSystemType(stystemType);
        }
        Example example=Example.of(appVersionInfo);
        Sort sort=new Sort(Sort.Direction.DESC,"createDate");
        List<AppVersionInfo> appVersionInfos = appVersionRepository.findAll(example,sort);
        List<AppVersionVO> appVersionVOS=new ArrayList<>();
        for (AppVersionInfo appVersionInfo1:appVersionInfos) {
            AppVersionVO appVersionVO=new AppVersionVO();
            BeanUtils.copyProperties(appVersionInfo1,appVersionVO);
            //如果是android，需要添加IP前缀
            String downloadUrl=appVersionVO.getDownloadUrl();
            if(appVersionInfo1.getSystemType().equals("1")){
                downloadUrl = global.file_public_ip+appVersionVO.getDownloadUrl();
            }
            appVersionVO.setDownloadUrl(downloadUrl);
            appVersionVO.setStystemTypeValue(StateToValueUtils.switchAppVersionValue(appVersionVO.getSystemType()));
            appVersionVO.setCreateDate(appVersionInfo1.getUpdateDate().getTime());
            appVersionVOS.add(appVersionVO);
        }
        logger.info("==findByList end");
        return ResultVOUtil.success(appVersionVOS);
    }
    /**
     * 更新app版本信息
     *
     */
    @Transactional
    public ResultVO updateAppVersion(AppVersionBean appVersionBean){
        logger.info("==updateAppVersion start ");
        AppVersionInfo appVersionInfo=appVersionRepository.getOne(appVersionBean.getId());
        if(null!=appVersionBean.getVersionNumber()){
            appVersionInfo.setVersionNumber(appVersionBean.getVersionNumber());
        }
        if(StringUtils.isNotBlank(appVersionBean.getVersionName())){
            appVersionInfo.setVersionName(appVersionBean.getVersionName());
        }
        if(StringUtils.isNotBlank(appVersionBean.getVersionDesc())){
            appVersionInfo.setVersionDesc(appVersionBean.getVersionDesc());
        }
        if(StringUtils.isNotBlank(appVersionBean.getSystemType())){
            appVersionInfo.setSystemType(appVersionBean.getSystemType());
        }
        if(StringUtils.isNotBlank(appVersionBean.getDownloadUrl())){
            appVersionInfo.setDownloadUrl(appVersionBean.getDownloadUrl());
        }
        if(StringUtils.isNotBlank(appVersionBean.getRemarks())){
            appVersionInfo.setRemarks(appVersionBean.getRemarks());
        }
        if(StringUtils.isNotBlank(appVersionBean.getUpdateType())){
            appVersionInfo.setUpdateType(appVersionBean.getUpdateType());
        }
        appVersionInfo.setUpdateDate(new Date());
        appVersionRepository.save(appVersionInfo);
        logger.info("==updateAppVersion end");
        return ResultVOUtil.success();
    }
    /**
     * 删除app版本信息
     *
     */
    @Transactional
    public ResultVO deleteAppVersion(Long id){
        logger.info("==deleteAppVersion id={} ",id);
        AppVersionInfo appVersionInfo=appVersionRepository.getOne(id);
        if(null==appVersionInfo){
            return ResultVOUtil.error(ResponseConstant.ERROR,"数据不存在");
        }
        appVersionInfo.setDelFlag("1");
        appVersionInfo.setUpdateDate(new Date());
        appVersionRepository.save(appVersionInfo);
        return ResultVOUtil.success();
    }
    /**
     * 添加app版本信息
     *
     */
    @Transactional
    public ResultVO addAppVersion(AppVersionBean appVersionBean){
        logger.info("==addAppVersion appVersionBean={} ",GsonUtils.getInstance().toJson(appVersionBean));
        AppVersionInfo appVersionInfo=new AppVersionInfo();
        BeanUtils.copyProperties(appVersionBean,appVersionInfo);
        appVersionInfo.setUpdateDate(new Date());
        appVersionInfo.setCreateDate(new Date());
        appVersionRepository.save(appVersionInfo);
        return ResultVOUtil.success();
    }

}
